$(function () {
    var thisPage = null,
        user = null,
        myscroll = null,
        pullUp = null,
        pullDown = null,
        html = [],
        query = {index: 0};

    var addressPage = function () {
        this.init();
    };

    addressPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {

            user = getLocalStorage(STORAGE_MANAGER.user);
            if (user) {
                this.addScroll();
                this.loadAddress();
            } else {
                redirect("../account/login.html");
                return false;
            }
        },
        addScroll: function () {
            pullUp = $("#pullUp");
            pullDown = $("#pullDown");
            myscroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                shrinkScrollbars: 'scale',
                click: true,
                keyBindings: true,
                momentum: true
            });
            myscroll.on("scrollEnd", function () {
                //console.log("this.y=" + this.y + " this.maxScrollY=" + this.maxScrollY );
                if ((this.y < 0) && (this.y == this.maxScrollY)) {
                    console.log("滚动到底部");
                    pullUp.show();
                    setTimeout(function () {
                        query.index++;
                        thisPage.loadAddress();
                    }, 500);
                } else if ((this.y == 0) && (this.y != this.maxScrollY)) {
                    console.log("滚动到顶部");
                    pullDown.show();
                    setTimeout(function () {
                        query.index--;
                        if (query.index < 0) {
                            query.index = 0;
                        }
                        ;
                        thisPage.loadAddress();
                    }, 500);
                }
            });
        },
        loadAddress: function () {

            $.ajax({
                type: "GET",
                url: CONFIG_MANAGER.RequstUrl + "v1/member/address/" + user.id,
                async: true,
                headers: {"token": user.token},
                beforeSend: function () {
                    pullDown.show();
                }
            }).always(function () {
                pullDown.hide();
            }).done(function (data) {
                thisPage.loadList(data);
            }).fail(function () {
                $.toast("请求出错");
            });

            //加载完成后刷新并隐藏上拉下拉提示
            setTimeout(function () {
                myscroll.refresh();
                pullUp.hide();
                pullDown.hide();
            }, 100);
        },
        loadList: function (data) {
            var list = JSON.parse(data), i = 0, list = list.data, html = [];
            while (list[i]) {
                html.push('<div class="shdzbg" tip=' + list[i].id + '>');
                html.push('<div class="shdzk' + (list[i].default_address ? '' : '1' ) + ' mbtopbottom">');
                html.push('<p class="dwei"><img src="../../images/ditutb' + (list[i].default_address ? '' : '1') + '.png"></p>');
                html.push('<div class="shdzhi">');
                html.push('<p class="sjrmc"><span>' + list[i].mobile + '</span><b class="mrziti' + (list[i].default_address ? '' : '1') + '">' + (list[i].default_address ? '[默认]' : '') + '</b>收件人：' + list[i].consignee + '</p>');
                html.push('<p>' + list[i].region_text + ' ' + list[i].address + '</p>');
                html.push('</div>');
                html.push('</div>');
                html.push('</div>');
                i++;
            }
            $("#addressList").html(html.join(''));
        },
        bindEvent: function () {

            $("#back").on("tap", function () {
                back();
            });

            //点击添加地址
            $("#addAddress").on("tap", function () {
                redirect("addAddress.html");
                return false;
            });

            //点击编辑
            $("#addressList").on("click", "div.shdzbg", function () {
                var tip = $(this).attr('tip');
                redirect("addAddress.html?id=" + tip);
                return false;
            });
        }
    };

    thisPage = new addressPage();

});
